package paneles;

import java.awt.FlowLayout;
import java.text.NumberFormat;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

import accesoADatos.AccesoADatos;
import datos.Producto;
import datos.Proveedor;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class PanelProductos extends javax.swing.JPanel {
	
	private JLabel lNombre;
	private JTextField tNombre;
	private JTextField jTextField1;
	private JLabel jLabel2;
	private JComboBox cbProveedores;
	private JLabel jLabel1;
	private JButton bCancelar;
	private JFormattedTextField tPrecioCosto;
	private JButton bAceptar;
	private JTextField tCantidadMinima;
	private JFormattedTextField tPrecioVenta;
	private JTextField tMarca;
	private JTextField tDescripcion;
	private JLabel lCantidad;
	private JLabel lPrecioVenta;
	private JLabel lPrecioCosto;
	private JLabel lMarca;
	private JLabel lDescripcion;
	private Hashtable hash = new Hashtable();

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new PanelProductos());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public PanelProductos() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			FlowLayout thisLayout = new FlowLayout();
			thisLayout.setHgap(10);
			thisLayout.setVgap(10);
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(299, 297));
			//this.setBackground(new java.awt.Color(0,0,0));
			{
				lNombre = new JLabel();
				this.add(getJLabel2());
				this.add(getJTextField1());
				this.add(lNombre);
				lNombre.setText("Nombre");
				//lNombre.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, true));
				lNombre.setPreferredSize(new java.awt.Dimension(110, 21));
				lNombre.setHorizontalAlignment(SwingConstants.CENTER);
				//lNombre.setForeground(new java.awt.Color(255,128,0));
			}
			{
				tNombre = new JTextField();
				this.add(tNombre);
				tNombre.setPreferredSize(new java.awt.Dimension(150, 21));
				//tNombre.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, false));
			}
			{
				lMarca = new JLabel();
				this.add(lMarca);
				lMarca.setText("Marca");
				//lMarca.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, true));
				lMarca.setPreferredSize(new java.awt.Dimension(110, 21));
				lMarca.setHorizontalAlignment(SwingConstants.CENTER);
				//lMarca.setForeground(new java.awt.Color(255,128,0));
			}
			{
				tMarca = new JTextField();
				this.add(tMarca);
				tMarca.setPreferredSize(new java.awt.Dimension(150, 21));
				//tMarca.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, false));
			}
			{
				lDescripcion = new JLabel();
				this.add(lDescripcion);
				lDescripcion.setText("Descripcion");
				//lDescripcion.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, true));
				lDescripcion.setPreferredSize(new java.awt.Dimension(110, 21));
				lDescripcion.setHorizontalAlignment(SwingConstants.CENTER);
				//lDescripcion.setForeground(new java.awt.Color(255,128,0));
			}
			{
				tDescripcion = new JTextField();
				this.add(tDescripcion);
				this.add(getJLabel1());
				this.add(getCbProveedores());
				tDescripcion.setPreferredSize(new java.awt.Dimension(150, 21));
				//tDescripcion.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, false));
			}
			{
				lPrecioCosto = new JLabel();
				this.add(lPrecioCosto);
				lPrecioCosto.setText("Precio Costo");
				//lPrecioCosto.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, true));
				lPrecioCosto.setPreferredSize(new java.awt.Dimension(110, 21));
				lPrecioCosto.setHorizontalAlignment(SwingConstants.CENTER);
				//lPrecioCosto.setForeground(new java.awt.Color(255,128,0));
			}
			{	
				tPrecioCosto = new JFormattedTextField();
				NumberFormat vFormat = NumberFormat.getCurrencyInstance();
				tPrecioCosto.setValue(new Double(0));
				NumberFormat eFormat = NumberFormat.getNumberInstance(Locale.ENGLISH);
				eFormat.setGroupingUsed(false);
				NumberFormatter dvFormat = new NumberFormatter(vFormat);
				NumberFormatter deFormat = new NumberFormatter(eFormat);
				DefaultFormatterFactory fact = new DefaultFormatterFactory(dvFormat,dvFormat,deFormat);
				tPrecioCosto.setFormatterFactory(fact);
				tPrecioCosto.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
				this.add(tPrecioCosto);
				tPrecioCosto.setPreferredSize(new java.awt.Dimension(150, 21));
				//tPrecioCosto.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, false));
			}
			{
				lPrecioVenta = new JLabel();
				this.add(lPrecioVenta);
				lPrecioVenta.setText("Precio Venta");
				//lPrecioVenta.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, true));
				lPrecioVenta.setPreferredSize(new java.awt.Dimension(110, 21));
				lPrecioVenta.setHorizontalAlignment(SwingConstants.CENTER);
				//lPrecioVenta.setForeground(new java.awt.Color(255,128,0));
			}
			{
				tPrecioVenta = new JFormattedTextField();
				NumberFormat vFormat = NumberFormat.getCurrencyInstance();
				tPrecioVenta.setValue(new Double(0));
				NumberFormat eFormat = NumberFormat.getNumberInstance(Locale.ENGLISH);
				eFormat.setGroupingUsed(false);
				NumberFormatter dvFormat = new NumberFormatter(vFormat);
				NumberFormatter deFormat = new NumberFormatter(eFormat);
				DefaultFormatterFactory fact = new DefaultFormatterFactory(dvFormat,dvFormat,deFormat);
				tPrecioVenta.setFormatterFactory(fact);
				tPrecioVenta.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
				this.add(tPrecioVenta);
				tPrecioVenta.setPreferredSize(new java.awt.Dimension(150, 21));
				//tPrecioVenta.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, false));

			}
			{
				lCantidad = new JLabel();
				this.add(lCantidad);
				lCantidad.setText("Cantidad Minima");
				//lCantidad.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, true));
				lCantidad.setPreferredSize(new java.awt.Dimension(109, 21));
				lCantidad.setHorizontalAlignment(SwingConstants.CENTER);
				//lCantidad.setForeground(new java.awt.Color(255,128,0));
			}
			{
				tCantidadMinima = new JTextField();
				this.add(tCantidadMinima);
				tCantidadMinima.setPreferredSize(new java.awt.Dimension(150, 21));
				//tCantidadMinima.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, false));
			}
			{
				bAceptar = new JButton();
				this.add(bAceptar);
				bAceptar.setText("Aceptar");
				bAceptar.setPreferredSize(new java.awt.Dimension(100, 25));
				//bAceptar.setBackground(new java.awt.Color(255,128,0));
			}
			{
				bCancelar = new JButton();
				this.add(bCancelar);
				bCancelar.setText("Cancelar");
				bCancelar.setPreferredSize(new java.awt.Dimension(100, 25));
				//bCancelar.setBackground(new java.awt.Color(255,128,0));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Producto getProducto(){
		Producto p = new Producto();
		p.setNombre(this.tNombre.getText());
		p.setDescripcion(this.tDescripcion.getText());
		if (this.tCantidadMinima.getText().equals(""))
			p.setCantidaMinima(0);
		else
			p.setCantidaMinima(new Integer(this.tCantidadMinima.getText()));
		p.setMarca(this.tMarca.getText());
		if (this.tPrecioCosto.getText().equals(""))
			p.setPrecioCosto(new Double(0));
		else
			p.setPrecioCosto(Double.valueOf(this.tPrecioCosto.getValue().toString()));
		if (this.tPrecioVenta.getText().equals(""))
			p.setPrecioVenta(new Double(0));
		else
			p.setPrecioVenta(Double.valueOf(tPrecioCosto.getValue().toString()));	
		p.setPrecioCosto(Double.valueOf(this.tPrecioCosto.getValue().toString()));
		p.setPrecioVenta(Double.valueOf(tPrecioVenta.getValue().toString()));
		if(this.cbProveedores.getSelectedItem().toString() == "Ninguno")
			p.setProveedor(null);
		else
			p.setProveedor((Proveedor)hash.get(this.cbProveedores.getSelectedItem().toString()));
		p.setCodigoBarra(this.jTextField1.getText());
		return p;
	}
	
	public void setProducto (Producto p){
		this.tNombre.setText(p.getNombre());
		this.tDescripcion.setText(p.getDescripcion());
		this.tMarca.setText(p.getMarca());
		this.tPrecioCosto.setValue(p.getPrecioCosto());
		this.tPrecioVenta.setText(new Double(p.getPrecioVenta()).toString());
		this.tCantidadMinima.setText(p.getCantidaMinima().toString());
		Proveedor prov = p.getProveedor();
		if(p.getProveedor()!= null)
			this.getCbProveedores().getModel().setSelectedItem(p.getProveedor().toString());
		else
			this.getCbProveedores().setSelectedIndex(0);
		this.jTextField1.setText(p.getCodigoBarra().toString());
	}
	
	private JLabel getJLabel1() {
		if(jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("Proveedor");
			jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel1.setPreferredSize(new java.awt.Dimension(110, 21));
			//jLabel1.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, true));
			//jLabel1.setForeground(new java.awt.Color(255,128,0));
		}
		return jLabel1;
	}
	
	@SuppressWarnings("unchecked")
	private JComboBox getCbProveedores() {
		if(cbProveedores == null) {
			cbProveedores = new JComboBox();
			cbProveedores.addItem("Ninguno");
			List l = AccesoADatos.getInstance().getProveedores();
			for(int i = 0; i<l.size(); i++){
				String s=l.get(i).toString();
				this.hash.put(l.get(i).toString(), l.get(i));
				cbProveedores.addItem(((Proveedor)l.get(i)).toString());
			}
			/*ComboBoxModel cbProveedoresModel = 
				new DefaultComboBoxModel(
						l.toArray());
			
			cbProveedores.setModel(cbProveedoresModel);*/
			cbProveedores.setPreferredSize(new java.awt.Dimension(150, 21));
			//cbProveedores.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, false));
			//cbProveedores.setBackground(new java.awt.Color(255,255,255));
		}
		return cbProveedores;
	}
	public JButton getBCancelar() {
		return bCancelar;
	}

	public JButton getBAceptar() {
		return bAceptar;
	}
	
	private JLabel getJLabel2() {
		if(jLabel2 == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("Codigo");
			jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel2.setPreferredSize(new java.awt.Dimension(109, 21));
			//jLabel2.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, true));
			//jLabel2.setForeground(new java.awt.Color(255,128,0));
		}
		return jLabel2;
	}
	
	private JTextField getJTextField1() {
		if(jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setPreferredSize(new java.awt.Dimension(150,21));
			//jTextField1.setBorder(new LineBorder(new java.awt.Color(255,128,0), 1, false));
		}
		return jTextField1;
	}

	public void setCodigo(String codigo) {
		this.jTextField1.setText(codigo);	
		this.repaint();
	}

}
